package work2;

import Uti.ScannerUti;

import java.util.ArrayList;
import java.util.Collections;

public class GradeAdmin {
    ArrayList<Grade> list = new ArrayList<>();
    GradeAdmin() {

        while (true) {
            System.out.println("-------学生管理系统-------");
            System.out.println("1---成绩录入");
            System.out.println("2---学生成绩打印");
            System.out.println("3---打印总成绩");
            System.out.println("0---退出");
            int kk = ScannerUti.getInt();
            if (kk == 1) {
                SomeGradeEntry();
            } else if (kk == 2) {
                GradePrintf();
            } else if (kk == 3) {
                while (true) {
                    System.out.println("选择排序方式：1——升序；2——降序；3——退出");
                    kk = ScannerUti.getInt();
                    if (kk == 1) {
                        Collections.sort(list, (o1, o2) -> (o1.allScore > o2.allScore) ? 1 : ((o1.allScore == o2.allScore) ? 0 : -1));
                    } else if (kk == 2) {
                        Collections.sort(list);
                    } else if (kk == 3) {
                        break;
                    }
                    GradePrintf();
                }

            } else if (kk == 0) {
                break;
            } else {
                System.out.println("请重新输入：");
            }
        }

    }



    void SomeGradeEntry() {
        System.out.println("请输入学生姓名，java，html，sql的成绩\",\"号分开,输入n结束");
        while (true) {
            String s = ScannerUti.getSting();
            if(s.split(",").length==4){
                Grade grade = new Grade(s);
                list.add(grade);
            }else if(s.equals("n"))break;
            else System.out.println("请重新输入:");
        }

    }

    void GradePrintf() {
        int max = 6;
        for (Grade grade : list) {
            Math.max(max, grade.name.length());
        }
        System.out.println(String.format("%-" + (max - 2) + "s" + "%-7s%-7s%-7s%-5s%-7s",
                "姓名", "java", "sql", "html", "总分", "平均分"));
        /*
        aawed,16,22,334
as,17,55,44
ewsa,15,45,69
         */
        for (Grade s : list) {
            System.out.println(String.format("%-" + max + "s" + "%-7s%-7s%-7s%-7s%-7s",
                    s.getName(), s.getJavaScore(), s.getSqlScore()
                    , s.getHtmlScore(), s.getAllScore(), s.getAveScore()));
        }


    }


}
